<?php
namespace app\common\model;
use think\Db;
class Mail{
    static public $e='';
    /**
     * 初始化邮件发送类
     * @param unknown $config 邮件配置
     */
    static public function init( $config=[] ){
        $s = \app\common\model\Data::getSys(['smtp_host','smtp_port','smtp_username','smtp_password','email_name','return_email','return_name']);
        $c = [
            'host'     => $s['smtp_host'],
            'port'     => $s['smtp_port'],
            'username' => $s['smtp_username'],
            'password' => $s['smtp_password'],
            'email_name' => $s['email_name'],
            'return_email' => $s['return_email'],
            'return_name'  => $s['return_name'],
        ];
        $c = array_merge($c,$config);
        
        self::$e = new \PHPMailer\PHPMailer\PHPMailer(true);
        // 服务器设置
        self::$e->SMTPDebug = 0;                                    // 开启Debug
        self::$e->Host = $c['host'];                        // 服务器地址
        self::$e->Port = $c['port'];                                        // 端口
        self::$e->SMTPAuth = true;                             // 开启SMTP验证
        self::$e->CharSet='UTF-8';
        self::$e->Username = $c['username'];            // SMTP 用户名（你要使用的邮件发送账号）
        self::$e->Password = $c['password'];             // SMTP 密码
        self::$e->SMTPSecure = 'tls';                                // 开启TLS 可选
        self::$e->isSMTP();                                        // 使用SMTP
        
        //发件人信息
        self::$e->setFrom($c['username'],$c['email_name']);            // 来自
        
        #回复
        if($c['return_email']){
            self::$e->addReplyTo($c['return_email'], $c['return_name']);
        }
        
    }
    /**
     * 设置的发送邮件
     * @param string $uuid  发送标识
     * @param unknown $temp 替换变量
     */
    static public function sendMail($uuid='', $email='', $temp=[]){
        $tp = Db::name('email_tp')->where('status',0)->where('uuid',$uuid)->find();
        if(!$tp)
            return com_error(['email'=>'系统未设置邮件发送']);
        
        #邮件发送账号初始化
        $ac = Db::name('email_ac')->where('status',0)->where('id',$tp['emailac_id'])->find();
        if($ac){
            $config = [
                'host' => $ac['host'],
                'port' => $ac['port'],
                'username' => $ac['email'],
                'password' => $ac['password'],
                'email_name' => $ac['name'],
                'return_email' => $ac['return_email'],
                'return_name'  => $ac['return_name'],
            ];
            self::init($config);
        }

        #发送邮件内容
        $p = [
            'address'       => $email,
            'addresscc'     => [],
            'attach'        => [],
            'subject'       => email_replace($tp['subject'],$temp),
            'body'          => email_replace($tp['body'],$temp),
            'altbody'       => '',
        ];
        if($tp['attach'])
            $p['attach'] = [$tp['attach']];
        
        if($tp['addresscc'])
            $p['addresscc'] = [$tp['addresscc']]; 
        
        return self::send($p);
    }
    
    /**
     * 发送邮件
     */
    static public function send( $p = [], $config=[] ){
        if(!self::$e){
            self::init($config);   
        }   
        $temp = [
            'address'       => '',
            'addresscc'     => [],
            'attach'        => [],
            'subject'       => '',
            'body'          => '',
            'altbody'       => '',
        ];
        $p = array_merge( $temp, array_intersect_key($p,$temp) );
        #发件
        if( $p['address'] ) 
            self::$e->addAddress($p['address']);
        
        #抄送
        foreach($p['addresscc'] as $v){
            self::$e->addCC( $v );
        }

        #附件
        foreach($p['attach'] as $k => $v){
            if(is_string($k)){
                self::$e->addAttachment( $v, $k);
            }else{
                self::$e->addAttachment( $v );
            }
        }
        
        self::$e->isHTML(true);
        if($p['subject']) self::$e->Subject = $p['subject'];
        if($p['body'])    self::$e->Body = $p['body'];
        if($p['altbody']) self::$e->AltBody = $p['altbody'];
        return self::$e->send();
    }
}